package com.ruoyi.generator.callback;

import com.ruoyi.generator.domain.MqttProperty;
import com.ruoyi.generator.util.MQTTClient;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class MqttCallback implements MqttCallbackExtended {
    public MqttCallback(MQTTClient mqttClient) {
        this.mqttClient = mqttClient;
        MqttCallbackActuator.init(mqttClient);
    }
    private final MQTTClient mqttClient;

    @Override
    public void connectComplete(boolean reconnect, String serverURI) {
        MqttCallbackActuator.connectComplete(reconnect, serverURI, mqttClient.getMqttProperty().getKey());
        for (String topic : mqttClient.getMqttProperty().getTopic()) {
            mqttClient.subscribe(topic, 0);
        }
    }

    @Override
    public void connectionLost(Throwable cause) {
        MqttCallbackActuator.connectionLost(cause, mqttClient.getMqttProperty().getKey());
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        MqttCallbackActuator.messageArrived(topic, message, mqttClient.getMqttProperty().getKey());
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        MqttCallbackActuator.deliveryComplete(token, mqttClient.getMqttProperty().getKey());
    }
}
